package com.gamota.mobi;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import com.gamota.mobi.adapter.ErrorHandler;
import com.gamota.mobi.background.BackgroundTasks;
import com.gamota.mobi.event.Event.RequestFailEvent;
import com.gamota.mobi.event.Event.UserTransactionSuccessEvent;
import com.gamota.mobi.model.UserTransaction;
import com.gamota.mobi.util.Utils;
import com.gamota.mobi.view.LoadMoreListView;
import com.gamota.mobi.view.LoadMoreListView.OnLoadMoreListener;

import de.greenrobot.event.EventBus;

public class TransactionLogActivity extends SherlockListActivity implements OnLoadMoreListener{
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		EventBus.getDefault().unregister(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		EventBus.getDefault().register(this);
	}

	private LayoutInflater inflater;
	private TransactionAdapter adapter;
	private List<UserTransaction> transList;
	private ProgressBar pbar;
	private TextView status;
	private LoadMoreListView listView;
	private String accessToken;
	private int start = 0;
	private BackgroundTasks bgTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_trans_log);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setTitle(R.string.transaction_log);
		inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		transList = new ArrayList<UserTransaction>();
		adapter = new TransactionAdapter(this, R.layout.transaction_item, transList);
		listView = (LoadMoreListView) getListView();
		listView.setAdapter(adapter);
		listView.setOnLoadMoreListener(this);
		pbar = (ProgressBar) findViewById(R.id.loading_progress);
		status = (TextView) findViewById(R.id.loading_status);
		accessToken = Utils.getStaticAccessToken(this);
		bgTask = new BackgroundTasks(this);
		bgTask.getTransactionLog(accessToken, start);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private class TransactionAdapter extends ArrayAdapter<UserTransaction> {

		public TransactionAdapter(Context context, int textViewResourceId, List<UserTransaction> objects) {
			super(context, textViewResourceId, objects);
			// TODO Auto-generated constructor stub
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if(convertView == null){
				convertView = inflater.inflate(R.layout.transaction_item, null);
				holder = new ViewHolder();
				holder.date = (TextView) convertView.findViewById(R.id.txt_date);
				holder.reason = (TextView) convertView.findViewById(R.id.txt_reason);
				holder.tym = (TextView) convertView.findViewById(R.id.txt_tym);
				holder.tymType = (ImageView) convertView.findViewById(R.id.img_trans_tym);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			UserTransaction ut = getItem(position);
			if(ut != null){
				holder.date.setText(getResources().getString(R.string.date) + " " + ut.getDate());
				holder.reason.setText(getResources().getString(R.string.reason) + " " + ut.getReason());
				if(!ut.getTym().equals("0")){
					holder.tym.setText(getResources().getString(R.string.tym_trans) + " " + ut.getTym());
					holder.tymType.setImageResource(R.drawable.tym_green);
				} else if(!ut.getTym2().equals("0")){
					holder.tym.setText(getResources().getString(R.string.tym_trans) + " " + ut.getTym2());
					holder.tymType.setImageResource(R.drawable.tym_purple);
				} else {
					holder.tym.setText(getResources().getString(R.string.tym_trans) + " 0");
				}
			}
			return convertView;
		}
	}

	public static class ViewHolder{
		TextView reason;
		TextView tym;
		TextView date;
		ImageView tymType;
	}
	
	public void onEventMainThread(UserTransactionSuccessEvent e){
		List<UserTransaction> list = e.getList();
		if(list.size() == 0){
			listView.setVisibility(View.GONE);
			pbar.setVisibility(View.GONE);
			status.setVisibility(View.VISIBLE);
			status.setText(R.string.no_trans);
			adapter.notifyDataSetChanged();
			listView.onLoadMoreComplete();
			listView.setOnLoadMoreListener(null);
		} else {
			for(UserTransaction ut : list){
				transList.add(ut);
			}
			listView.setVisibility(View.VISIBLE);
			pbar.setVisibility(View.GONE);
			status.setVisibility(View.GONE);
			adapter.notifyDataSetChanged();
			listView.onLoadMoreComplete();
		}
	}
	
	public void onEventMainThread(RequestFailEvent e){
		listView.setVisibility(View.GONE);
		pbar.setVisibility(View.GONE);
		status.setVisibility(View.GONE);
		listView.setOnLoadMoreListener(null);
		ErrorHandler.getInstance().setContext(this).handleError(e.getErrorCode());
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		start = start + 20;
		bgTask.getTransactionLog(accessToken, start);
	}
}
